package com.framework.MessageConverter;

import org.apache.activemq.command.ActiveMQMessage;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

import javax.jms.*;
import javax.xml.soap.Text;
import java.io.Serializable;

public class UserMessageConverter implements MessageConverter {

    @Override
    public Message toMessage(Object object, Session session)
            throws JMSException, MessageConversionException {
        return session.createObjectMessage((Serializable) object);
    }

    public Object fromMessage(Message message) throws JMSException,
            MessageConversionException {
        if(message instanceof TextMessage){
            TextMessage textMessage = (TextMessage) message;
            return  textMessage.getText();
        }
        if(message instanceof ObjectMessage){
            ObjectMessage objMessage = (ObjectMessage) message;
            return objMessage.getObject();
        }
        return  message;
    }
}
